<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\LoginLogModel;
use app\admin\service\LoginLogService;
use app\common\libs\phpspreadsheet\PhpSpreadSheet;
use app\Request;
use think\facade\View;

class Log extends AdminController
{
    /**
     * 登陆日志列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function loginLog(Request $request){
        if($request->param('_action') == 'loginLog'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $roleList = LoginLogService::loginLog($params,$page,$limit);
            return json($roleList);
        }
        return View::fetch('loginLog');
    }

    /**
     * 操作日志列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function operationLog(Request $request){
        if($request->param('_action') == 'operationLog'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $roleList = LoginLogService::operationLog($params,$page,$limit);
            return json($roleList);
        }
        return View::fetch('operationLog');
    }

    /**
     * 删除登陆日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function deleteLoginLog(){
        $result = LoginLogService::deleteLoginLog();
        return json($result);
    }

    /**
     * 登陆日志导出
     *
     * @Author tanruizheng
     * @DateTime 2021-07-27
     * @return void
     */
    public function logExport(){
        $header = [
            '用户名',
            '密码',
            '状态',
            '登陆时间'
        ];
        $data = LoginLogModel::where([])->limit(100)->field('username,password,status,logintime')->select()->toArray();
        foreach ($data as &$item) {
            $item['logintime'] = date('Y-m-d H:i:s',$item['logintime']);
            $item['status'] = $item['status'] ? '成功' : '失败';
        }
        PhpSpreadSheet::instance()
            ->setExcelHeader($header)
            ->setExcelTitle('用户登陆日志')
            ->setExcelContent($data)
            ->excelSave('用户登陆日志','xlsx');
    }

    /**
     * 删除操作日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function deleteOperationLog(){
        $result = LoginLogService::deleteOperationLog();
        return json($result);
    }
}
